<template>
    <el-container class="home-container">
<!--      头部区域-->
      <el-header>
        <div class="touxiang">
          <img src="../assets/zhangyuge.jpeg" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
<!--      页面主体区域-->
      <el-container>
<!--        侧边栏-->
        <el-aside :width="isCollapse ? '64px':'200px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
<!--          侧边栏菜单 :router开启路由模式 :collapse折叠模式 :collapse-transition折叠动画模式
              :default-active默认状态激活模式-->
          <el-menu background-color="#0086b3"
                   text-color="#333"
                   active-text-color="#ffd04b"
                   unique-opened
                   :collapse="isCollapse"
                   :collapse-transition="false"
                   :router="true"
                   :default-active="activePath">
<!--            一级菜单-->
<!--            :index动态绑定数字，但是只接受字符串，所以+''
                 :class="iconsObj[item.id]动态获取图标-->
            <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
<!--              一级菜单模板区域-->
              <template slot="title">
<!--                图标-->
                <i :class="iconsObj[item.id]"></i>
<!--                文本-->
                <span>{{item.authName}}</span>
              </template>
<!--             二级菜单 :index="'/'+subItem.path"路由的跳转地址-->
              <el-menu-item :index="'/'+subItem.path"
                            v-for="subItem in item.children"
                            :key="subItem.id"
                            @click="saveNavState('/'+subItem.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
<!--        右侧内容主体-->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

</template>

<script>
  export default {
    name: 'Home',
    data(){
      return{
        //左侧菜单数据
        menulist:[],
        iconsObj:{
          '125':'iconfont icon-user',
          '103':'iconfont icon-tijikongjian',
          '101':'iconfont icon-shangpin',
          '102':'iconfont icon-danju',
          '145':'iconfont icon-baobiao',
        },
        // 是否折叠
        isCollapse:false,
        // 被激活的链接地址
        activePath:''

      }
    },
    created() {
      this.getMenuList()
      this.activePath=window.sessionStorage.getItem('activePath')
      console.log(this.activePath)
    },
    methods:{
      //退出功能
      logout(){
        //清空token
        window.sessionStorage.clear();
        //跳转到登录页面
        this.$router.push('/login')
      },
      // 获取所有的菜单
      async getMenuList(){
        const {data:res}=await this.$http.get('menus')
        if(res.meta.status!==200) return this.$message.error(res.meta.msg);
        this.menulist=res.data;
      },
      // 点击按钮，切换菜单的折叠与展开
      toggleCollapse(){
        this.isCollapse=!this.isCollapse;
      },
      //保存链接的激活状态
      saveNavState(activePath){
        window.sessionStorage.setItem('activePath',activePath)
        // 获取激活地址
        this.activePath=activePath;
        console.log(this.activePath)
      }
    },

  }
</script>

<style lang="less" scoped>
  .home-container{
    /*display: flex;*/
    height: 100%;
  }
  .el-header{
    display: flex;
    height: 100px;
    justify-content: space-between;
    background-color: #0086b3;
    padding-left: 10px;
    align-items: center;
    /*color: #3333;*/
  }
  .touxiang{
     display: flex;
     /*position: relative;*/
     align-items: center;
     width: 200px;
     height: 60px;
     border-radius: 50%;
     /*background-color: pink;*/
     font-size: 16px;
     font-weight: 500;
     color: #333333;
   }
  img{
    width: 56px;
    height: 56px;
    border: 1px solid #0086b3;
    border-radius: 50%;
    margin: 2px 0;
  }
  span{
    height: 30px;
    margin-left: 5px;
  }

  .el-aside{
    background-color: #0086b3;
  }
  .iconfont{
    margin-right: 10px;
    /*background-color: #333333;*/
  }
  .toggle-button{
    background-color: #2b4b6b;
    font-size: 10px;
    font-weight: 600;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor:pointer;
  }
  .el-main{
    /*flex: 1;*/
    /*height: 100%;*/
    /*overflow: auto;*/
    background-color: #eee;

  }

</style>
